﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.CommonFilter;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Core.Metadata.EntityElement;
using Kingdee.BOS.Core.Metadata.FieldElement;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.Core;
using Kingdee.K3.FIN.Core.Object.Acctg;
using Kingdee.K3.FIN.Core.Parameters;
using Kingdee.K3.FIN.HS.ServiceHelper;
using Kingdee.K3.SCM.ServiceHelper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;

namespace CB.KM.ZH.SettleBillUpdateSettlePrice
{
    [Description("结算单更新结算单价"), HotUpdate]
    public class SalReturnUpdateCost : AbstractDynamicFormPlugIn
    {
        public string LogType { get; set; } = "结算单更新结算单价";
        public override void AfterEntryBarItemClick(AfterBarItemClickEventArgs e)
        {
            base.AfterEntryBarItemClick(e);
            if (e.BarItemKey.EqualsV1("SSYF_JSJG"))
            {
                try
                {
                    EntryEntity entydataObj = this.View.BusinessInfo.GetEntryEntity("FResultEntity");
                    DynamicObjectCollection entyDatas = this.View.Model.GetEntityDataObject(entydataObj);
                    if (entyDatas == null)
                        return;
                    var dataList = entyDatas.Where(x=>x["IsChoosed"].ToStringV1().ToBoolean()&& x["F_SSYF_BL"].ToDecimalV1()!=0).Select(x => new
                    {
                        ser=x["RowId"].ToInt32V1()-1,
                        settlePrice=x["Price"].ToDecimalV1(),
                        rate=x["F_SSYF_BL"].ToDecimalV1()
                    }).ToList();
                    if (dataList == null)
                        return;
                    foreach (var data in dataList)
                    {
                        Model.SetValue("F_SSYF_YDJ", data.settlePrice, data.ser);
                        View.UpdateView("F_SSYF_YDJ", data.ser);
                        decimal jsj = data.settlePrice * data.rate;
                        Model.SetValue("FPrice", jsj, data.ser);
                        View.UpdateView("FPrice", data.ser);
                    }
                    //View.UpdateView("FResultEntity");
                    //View.UpdateView("F_SSYF_YDJ");
                    //View.UpdateView("FPrice");
                }
                catch (Exception ex)
                {
                    View.ShowErrMessage(ex.Message);
                }

            }
        }
        public override void DataChanged(DataChangedEventArgs e)
        {
            base.DataChanged(e);
            if (e.Field.Key.EqualsV1("FPrice"))
            {
                decimal ovalue = e.OldValue.ToDecimalV1();
                Model.SetValue("F_SSYF_YDJ",ovalue,e.Row);
                View.UpdateView("F_SSYF_YDJ",e.Row);
            }
        }
    }
  
}
